package com.demo.cn.greendaodemo;

/**
 * Created by admin at 2018/11/1
 */

import com.demo.cn.greendaodemo.entity.DaoSession;
import com.demo.cn.greendaodemo.entity.User;
import com.demo.cn.greendaodemo.entity.UserDao;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin at 2018/11/1
 */

public class SQLiteUtils {
    private static SQLiteUtils instance;
    UserDao phoneContactsEntityDao;
    DaoSession daoSession;

    private SQLiteUtils() {
        phoneContactsEntityDao = MyApplication.getInstances().getDaoSession().getUserDao();
        daoSession = MyApplication.getInstances().getDaoSession();
    }

    public static SQLiteUtils getInstance() {
        if (instance == null) {
            synchronized (SQLiteUtils.class) {
                if (instance == null) {
                    instance = new SQLiteUtils();
                }
            }
        }
        return instance;
    }

    public DaoSession getDaoSession(){
        return daoSession;
    }

    //增加
    public void addContacts(User userBean) {
        phoneContactsEntityDao.insert(userBean);
    }

    //删除
    public void deleteContacts(User userBean) {
        phoneContactsEntityDao.delete(userBean);
    }

    //修改
    public void updateContacts(User userBean) {
        phoneContactsEntityDao.update(userBean);
    }

    //条件查询
    public List selectAllContacts() {
        phoneContactsEntityDao.detachAll();//清除缓存
        List list1 = phoneContactsEntityDao.queryBuilder().list();
        return list1 == null ? new ArrayList() : list1;
    }

    //删除表中内容
    public void deleteAllContact() {
        phoneContactsEntityDao.deleteAll();
    }
}

